package com.miyake.loveviewweb.client.viewer;

import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.DataServiceWrapper;
import com.miyake.loveviewweb.client.community.GroupListHandler;
import com.miyake.loveviewweb.client.community.GroupListWidget;
import com.miyake.loveviewweb.client.community.IGroupList;
import com.miyake.loveviewweb.client.dialog.BasicDialog;
import com.miyake.loveviewweb.client.main.UiDataHandler;
import com.miyake.loveviewweb.shared.model.ChartModel;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.GroupModel;
import com.miyake.loveviewweb.shared.model.ValueModel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;

public class DataViewer extends Viewer {
    @Override
    public void update(Key key) {
        if (DataModel.class.getName().contains(key.getKind())) {
            DataServiceWrapper.getInstance().getService().getData(key, false, new AsyncCallback<DataModel>() {
                public void onFailure(Throwable caught) {
    
                }
    
                public void onSuccess(DataModel result) {
                    dataModel = result;
                    superUpdate(result.getKey());
                }
                
            });
        }
        else if (ChartModel.class.getName().contains(key.getKind())) {
            DataServiceWrapper.getInstance().getService().getData(key.getParent(), false, new AsyncCallback<DataModel>() {
                public void onFailure(Throwable caught) {
    
                }
    
                public void onSuccess(DataModel result) {
                    dataModel = result;
                    superUpdate(result.getKey());
                }
                
            });
        }
    }

    private void superUpdate(Key key) {
        super.update(key);
    }
    
    @Override
    protected int getLevel() {
        return 2;
    }

    private DataModel dataModel;
    private UiDataHandler uiHandler;
    private ChartListWidget chartList;
    
    public DataViewer(UiDataHandler uiHandler) {
        this.uiHandler = uiHandler;
    }

    public DataViewer(DataModel dataModel, UiDataHandler uiHandler) {
        this.uiHandler = uiHandler;
        this.dataModel = dataModel;
    }

    @Override
    String getPageTitle() {
        return dataModel.getName();
    }

    @Override
    Widget getContent() {
        VerticalPanel chartPanel = new VerticalPanel();
        chartPanel.setWidth("100%");
        
        chartList = new ChartListWidget(dataModel.getKey()){
            @Override
            protected void onColumnCountChange(int column) {
                DataServiceWrapper.getInstance().getService().setChartColumns(dataModel.getKey(), column, new AsyncCallback<Void>() {

                    public void onFailure(Throwable caught) {
                    }

                    public void onSuccess(Void result) {
                    }
                    
                });
            }

            @Override
            protected Widget getAdditionalTool() {
                if (isToolBarVisible()) {
                    Button button = new Button("Add Chart");
                    button.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            uiHandler.createChart(dataModel);
                        }                   
                    });
                    return button;
                }
                else {
                    return new Label("");
                }
            }
            
        };
        chartList.setColumns(dataModel.getChartColumns());
        chartPanel.add(chartList);
        chartList.setVisible(dataModel.isChartListVisible());
        
        // Data
        chartPanel.add(new DataSummaryWidget(this.dataModel));
                
        return chartPanel;
    }

    @Override
    Widget getToolbar() {
        HorizontalPanel panel = new HorizontalPanel();
        panel.add(new PluginBar(this.dataModel) {

            @Override
            void onParse(Key key) {
                DataServiceWrapper.getInstance().parseFile(key, dataModel.getKey());
            }

            @Override
            void onAnalyze(Key key, List<ValueModel> values,
                    Map<String, String> data) {
                
                DataServiceWrapper.getInstance().analyze(key, values, data, dataModel.getKey());
            }
         
        });
        
        if (canCreateChart()) {
            CheckBox chartCheck = new CheckBox("Show charts");
            chartCheck.setValue(dataModel.isChartListVisible());
            panel.add(chartCheck);
            chartCheck.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
                public void onValueChange(ValueChangeEvent<Boolean> event) {
                    chartList.setVisible(event.getValue());
                    DataServiceWrapper.getInstance().getService().setChartListVisible(dataModel.getKey(), event.getValue(), new AsyncCallback<Void>() {
    
                        public void onFailure(Throwable caught) {
                            // TODO Auto-generated method stub
                            
                        }
    
                        public void onSuccess(Void result) {
                            
                        }
                        
                    });
                }
            });
        }
        
        if (canPublish()) {
            panel.add(new Button("Publish", new ClickHandler() {
                public void onClick(ClickEvent event) {
                    showGroupSelectDialog();
                }
            }));
        }
        return panel;
    }
    
    private boolean canCreateChart() {
        return dataModel.getDataType().equals(DataModel.DataType.ValueArray);
    }

    protected void showGroupSelectDialog() {
        
        GroupListWidget widget = new GroupListWidget(IGroupList.GroupListType.MyGroup);
        widget.update();
        final BasicDialog dlg = new BasicDialog(widget) {

            @Override
            protected void onOK(Composite composite) {

            }
            
        };
        dlg.showDialog();

        widget.setGroupListHandler(new GroupListHandler() {
            public void onSelect(GroupModel groupModel) {
                DataServiceWrapper.getInstance().getService().publish(dataModel.getKey(), groupModel.getKey(), new AsyncCallback<Void>() {
                    public void onFailure(Throwable caught) {

                    }

                    public void onSuccess(Void result) {

                    }
                });
            }
        });
    }
    
    protected boolean canPublish() {
        return true;
    }
}
